<script setup>
import {getCurrentInstance, onMounted, reactive, ref} from "vue";
import * as echarts from "echarts";

const {proxy} = getCurrentInstance()
const getImgUrl = (user) => {
  return new URL(`../assets/images/${user}.png`, import.meta.url).href
}
const tableLabel = ref({
  name: '课程',
  todayBuy: '今日购买',
  monthBuy: '本月购买',
  totalBuy: '总购买',
})

//这个tableData是假数据，等会我们使用axios请求mock数据
let tableData = ref([])
let countData = ref([])
let chartData = ref([])

//observer 接收观察器实例对象
const observer = ref(null)

//这个是折线图和柱状图 两个图表共用的公共配置
const xOptions = reactive({
  // 图例文字颜色
  textStyle: {
    color: "#333",
  },
  legend: {},
  grid: {
    left: "20%",
  },
  // 提示框
  tooltip: {
    trigger: "axis",
  },
  xAxis: {
    type: "category", // 类目轴
    data: [],
    axisLine: {
      lineStyle: {
        color: "#17b3a3",
      },
    },
    axisLabel: {
      interval: 0,
      color: "#333",
    },
  },
  yAxis: [
    {
      type: "value",
      axisLine: {
        lineStyle: {
          color: "#17b3a3",
        },
      },
    },
  ],
  color: ["#2ec7c9", "#b6a2de", "#5ab1ef", "#ffb980", "#d87a80", "#8d98b3"],
  series: [],
})

const pieOptions = reactive({
  tooltip: {
    trigger: "item",
  },
  legend: {},
  color: [
    "#0f78f4",
    "#dd536b",
    "#9462e5",
    "#a6a6a6",
    "#e1bb22",
    "#39c362",
    "#3ed1cf",
  ],
  series: []
})
const getTalData = async () => {
    let data = await proxy.$api.getTableDataApi();
    tableData.value=data.tableData
}

// count获取值
const getCountData = async () => {
  countData.value=await proxy.$api.getCountData()
}

const getChartData = async () => {
  let {orderData,userData,videoData} = await proxy.$api.getChartData();
  // console.log(orderData)
  //第一个图标的x轴和series赋值
  xOptions.xAxis.data=orderData.date
  xOptions.series=Object.keys(orderData.data[0]).map(val=>{
    return{
      name: val,
      data: orderData.data.map(item => item[val]),
      type: 'line'
    }
  })
  const oneEchart=echarts.init(proxy.$refs["echart"])
  oneEchart.setOption(xOptions)
  //第二个柱状图的x轴和series赋值
  xOptions.xAxis.data=userData.map(i=>i.date)
  xOptions.series=[
    {
      name: "新增用户",
      data: userData.map(i=>i.new),
      type: 'bar'
    },
    {
      name: "活跃用户",
      data: userData.map(i=>i.active),
      type: 'bar'
    }
  ]
  const userEchart=echarts.init(proxy.$refs["userEchart"])
  userEchart.setOption(xOptions)
  //对饼状图
  pieOptions.series=[
    {
      data: videoData,
      type: "pie"
    }
  ]

  const pieEchart=echarts.init(proxy.$refs['videoEchart'])
  pieEchart.setOption(pieOptions)

  //监听页面的变化
  observer.value= new ResizeObserver(()=>{
    oneEchart.resize()
    userEchart.resize()
    pieEchart.resize()
  })

  //容器存在
  if (proxy.$refs["echart"]){
    debugger

    const targetElement = document.querySelector(".home");
    observer.value.observe(targetElement)
  }
}


onMounted(() => {
  getTalData()
  getCountData()
  getChartData()

})

</script>

<template>
  <el-row :gutter='20' class="home">
    <el-col :span="8" style="margin-top: 20px">
      <el-card shadow="hover">
        <div class="user">
          <img :src="getImgUrl('user')" alt="">
          <div class="user-info">
            <p class="user-info-admin">Admin</p>
            <p class="user-info-p">超级管理员</p>
          </div>
        </div>
        <div class="login-info">
          <p>上次登录时间: <span>2024-07-02</span></p>
          <p>上次登录地点: <span>北京</span></p>
        </div>
      </el-card>
      <el-card shadow="hover" class="user-table">
        <el-table :data="tableData">
          <el-table-column
              v-for="(val,key) in tableLabel"
              :key="key"
              :prop="key"
              :label="val"
          >
          </el-table-column>
        </el-table>
      </el-card>
    </el-col>
    <el-col :span="16" style="margin-top: 20px">
       <div class="num">
         <el-card
             :body-style="{display:'flex',padding:0}"
             v-for="item in countData" :key="item.name"
         >
           <component :is="item.icon" class="icons" :style="{background:item.color}"></component>
           <div class="detail">
             <p class="num">￥{{item.value}}</p>
             <p class="txt">￥{{item.name}}</p>
           </div>
         </el-card>
       </div>
        <el-card class="top-echart">
         <div ref="echart" style="height: 280px"></div>
        </el-card>
        <div>
          <div class="graph">
            <el-card >
              <div ref="userEchart" style="height: 280px"></div>
            </el-card>
            <el-card >
              <div ref="videoEchart" style="height: 280px"></div>
            </el-card>
          </div>
        </div>
    </el-col>
  </el-row>
</template>

<style scoped lang="less">

.home {
  height: 100%;
  overflow: hidden;
  .num{
    justify-content: space-between;
    display: flex;
    flex-wrap: wrap;
    .el-card{
      width: 32%;
      margin-bottom: 20px;
    }
    .icons{
      width: 80px;
      height: 80px;
      font-size: 30px;
      text-align: center;
      line-height: 80px;
      color: #ffffff;
    }
    .detail{
     margin-left: 20px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      .num{
        font-size: 30px;
        margin-bottom: 10px;
      }
      .txt{
        font-size: 15px;
        text-align: center;
        color: #999999;
      }
    }
  }
  .user {
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
    margin-bottom: 20px;

    img {
      width: 150px;
      height: 150px;
      border-radius: 50%;
      margin-right: 40px;
      margin-bottom: 10px;
    }

    .user-info {
      p {
        line-height: 40px;
      }

      .user-info-p {
        color: #999;
      }

      .user-info-admin {
        font-size: 30px;
      }
    }

  }

  .login-info {
    margin-top: 20px;

    p {
      line-height: 30px;
      font-size: 15px;
      color: #999;

      span {
        color: #666;
        margin-left: 60px;
      }
    }
  }
  .graph{
    margin-top: 10px;
    display: flex;
    justify-content: space-between;
    .el-card{
      width: 48%;
      height: 310px;
    }
  }
}

.user-table {
  margin-top: 20px;
}

</style>
